﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PMC;
using PMC.Structs;

namespace PMCgui
{
    public partial class ExpressionsTab : UserControl
    {
        private DatagridPanel<Expressions> expressionsPanel;
        public ExpressionsTab()
        {
            InitializeComponent();
            Dock = DockStyle.Fill;
            Init();
        }

        /// <summary>
        /// Initialize the panel
        /// </summary>
        private void Init()
        {
            expressionsPanel = new DatagridPanel<Expressions>(UrlExpressions.Instance.Expressions, new[] { "Expression", "Type" });
            expressionsPanel.Datagridview.ReadOnly = true;
            pnlExpressions.Controls.Clear();
            expressionsPanel.Dock = DockStyle.Fill;
            pnlExpressions.Controls.Add(expressionsPanel);
        }

        /// <summary>
        /// Method bound to btnAddExp.Click event; Opens dialog to add an Expression
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddExp_Click(object sender, EventArgs e)
        {
            AddExpressions(ExpressionTypes.Expression);
            expressionsPanel.Refresh();
        }

        private void AddExpressions(ExpressionTypes type)
        {
            var dialog = new PromptForm();
            dialog.ShowDialog();
            if (!string.IsNullOrWhiteSpace(dialog.Text))
                UrlExpressions.Instance.Add(new Expressions(dialog.Input, type));
            expressionsPanel.Refresh();
        }

        /// <summary>
        /// Method bound to btnAddExc.Click event; Opens dialog to add an Exception
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddExc_Click(object sender, EventArgs e)
        {
            AddExpressions(ExpressionTypes.Exception);
            expressionsPanel.Refresh();
        }

        /// <summary>
        /// Method bound to btnDelete.Click event; delete selected Expressions
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Language.Instance.DeleteExpression, Language.Instance.Delete, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                foreach (DataGridViewRow row in expressionsPanel.Datagridview.SelectedRows)
                {
                    UrlExpressions.Instance.Remove((Expressions)row.DataBoundItem);
                }
            }
            expressionsPanel.Refresh();
        }

        /// <summary>
        /// Method bound to btnTestUrl.Click event; test a string to Expressions
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTestUrl_Click(object sender, EventArgs e)
        {
            var dialog = new PromptForm();
            dialog.ShowDialog();
            bool result = UrlExpressions.Instance.TestString(dialog.Text);
            var message = string.Format(Language.Instance.ThisLinkWillBeFoundDuringSearch, result ? Language.Instance.Yes.ToUpper() : Language.Instance.No.ToUpper(), result ? "" : "not"); //Todo culture invariant
            MessageBox.Show(message);
        }
    }
}
